package io.sxl.modules.generator.controller;

import java.util.*;

import io.sxl.modules.generator.entityx.BuildingEntity;
import io.sxl.modules.generator.entityx.DiaoBoEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.sxl.modules.generator.entity.HouseEntity;
import io.sxl.modules.generator.service.HouseService;
import io.sxl.common.utils.PageUtils;
import io.sxl.common.utils.R;

/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-03-26 20:06:25
 */
@RestController
@RequestMapping("generator/house")
@Slf4j
public class HouseController {
    @Autowired
    private HouseService houseService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = houseService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listhasfenpei")
    public R listIsFenpei(@RequestParam Map<String, Object> params) {

        PageUtils page = houseService.queryHasFenpei(params);
        return R.ok().put("page", page);
    }

    /**
     * 自定义查询showhouse方法
     */
    @RequestMapping("/listshow")
    public R listshow(@RequestParam Map<String, Object> params) {
        //将page和limit由Object类型转换成为Integer类型
        params.replace("page",Integer.parseInt((String)params.get("page")));
        params.replace("limit",Integer.parseInt((String)params.get("limit")));

        String key = (String)params.get("key");
        //判断为模糊查询还是全部查询
        if(!"".equals(key) && key!=null){
            //匹配数字
            if (key.matches("^[0-9]*$")) {
                params.put("integer",Integer.parseInt(key));
                PageUtils pageUtils = houseService.queryLikeNumber(params);
                return R.ok().put("page",pageUtils);
            }
            //匹配汉字
            if (key.matches("^[\\u4e00-\\u9fa5]*$")){
                params.put("str","%"+params.get("key")+"%");
                PageUtils pageUtils = houseService.queryLikeStr(params);
                return R.ok().put("page",pageUtils);
            }


            return R.ok().put("page","{null}");
        }else {
            int count = houseService.count();

            PageUtils page = houseService.queryShowHouse(params);
            page.setTotalCount(count);
            return R.ok().put("page", page);
        }
    }

    /**
     * 自定义查询page方法
     * @param params
     * @return
     */
    @RequestMapping("/queryByHousePage")
    public R queryByHousePage(@RequestParam Map<String, Object> params) {

        Integer count = houseService.queryByHousePageCount(params);

        System.out.println("count = " + count);

        params.replace("page",Integer.parseInt((String)params.get("page")));

        params.replace("limit",Integer.parseInt((String)params.get("limit")));

        PageUtils pageUtils = houseService.queryByHousePage(params);
        pageUtils.setTotalCount(count);
        return R.ok().put("page", pageUtils);
    }
    @RequestMapping("/queryByProIdAndHostIsnull")
    public R queryByProIdAndHostIsnull(@RequestParam Map<String,Object> params){

        Integer count = houseService.queryByProIdAndHostNotNullCount(params);

        params.replace("page",Integer.parseInt((String)params.get("page")));

        params.replace("limit",Integer.parseInt((String)params.get("limit")));

        PageUtils pageUtils = houseService.queryByProIdAndHostNotNull(params);

        pageUtils.setTotalCount(count);
        return R.ok().put("page",pageUtils);


    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:house:info")
    public R info(@PathVariable("id") Integer id) {
        HouseEntity house = houseService.getById(id);

        return R.ok().put("house", house);
    }



    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:house:save")
    public R save(@RequestBody HouseEntity house) {
        houseService.save(house);

        return R.ok();
    }

    /**
     * 保存整栋
     * （这里写的并不规范，可能之后需要把业务从控制层去除放到service层，有时间该没时间就没办法了）
     */
    @RequestMapping("/saveall")
    @RequiresPermissions("generator:house:save")
    public R saveAll(@RequestBody BuildingEntity buildingEntity) {
        List<String> huxing = buildingEntity.getHuxing();
        for (Integer i = 0; i < buildingEntity.getLoucengNumber(); i++) {
            for (Integer j = 0; j < buildingEntity.getDanyuanNumber(); j++) {
                for (Integer integer = 0; integer < buildingEntity.getHouseNumber(); integer++) {
                    HouseEntity houseEntity = new HouseEntity();
                    houseEntity.setSuqare(buildingEntity.getSuqaare().get(j));
                    houseEntity.setDongId(buildingEntity.getDongId());
                    String housenum = (i + 1) + "0" + (j * buildingEntity.getHouseNumber() + integer + 1);
                    houseEntity.setHouseNum(Integer.parseInt(housenum));
                    houseEntity.setHuxing(buildingEntity.getHuxing().get(j));
                    houseEntity.setXiaoqu(buildingEntity.getName());
                    boolean save = houseService.save(houseEntity);
                    System.out.println("save = " + save);

                }


            }
        }
        System.out.println(buildingEntity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:house:update")
    public R update(@RequestBody HouseEntity house) {
        //house.setHostId(null);
        houseService.updateById(house);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:house:delete")
    public R delete(@RequestBody Integer[] ids) {
        houseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
